package com.gzu.ltms.controller.address;


import com.gzu.ltms.entity.Address;
import com.gzu.ltms.entity.dto.AddressDTO;
import com.gzu.ltms.entity.dto.AddressUpdateDTO;
import com.gzu.ltms.response.Response;
import com.gzu.ltms.service.AddressService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    /**
     * 添加地址
     * @param addressDTO
     * @return
     */
    @PostMapping("/add")
    public Response<?> addAddress(@Validated @RequestBody AddressDTO addressDTO) {
        return addressService.addAddress(addressDTO);
    }

    /**
     * 修改地址
     * @param addressUpdateDTO
     * @return
     */
    @PutMapping("/update")
    public Response<?> update(@Validated @RequestBody AddressUpdateDTO addressUpdateDTO) {
        return addressService.updateAddress(addressUpdateDTO);
    }


    /**
     * 删除地址
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Response<?> delete(@RequestParam("id") String id) {
        addressService.removeById(id);
        return Response.success();
    }

    /**
     * 获取所有地址
     * @return
     */
    @GetMapping("/get")
    public Response<List<Address>> getAllAddress() {
        return addressService.getAllAddress();
    }

    /**
     * 根据id获取地址
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Response<Address> getAddressById(@PathVariable("id") String id) {
        Address address = addressService.getById(id);
        return Response.success(address);
    }

}
